﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using CodingTheTweet;
using System.Web;

namespace TwiXaml
{
    class Twitter
    {


        oAuthTwitter oAuth = new oAuthTwitter();

        public Twitter()
        {
            this.oAuth.Token = Settings1.Default.token;
            this.oAuth.TokenSecret = Settings1.Default.secretToken;
            this.oAuth.ConsumerKey = Settings1.Default.consumerKey;
            this.oAuth.ConsumerSecret = Settings1.Default.consumerSecret;
            this.oAuth.PIN = Settings1.Default.pin;
        }


        public bool Login(String username,String password)
        {
            bool Result = false;
            try
            {
           
            string UserPass = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
            ServicePointManager.Expect100Continue = false;
            HttpWebRequest req = WebRequest.Create("http://twitter.com/account/verify_credentials.format") as HttpWebRequest;
            req.Method = "GET";
            req.ContentType = "application/x-www-form-urlencoded";
            req.Headers.Add("Authorization", "Basic" + UserPass);
            HttpWebResponse WebResponse = (HttpWebResponse)req.GetResponse();
            Result = WebResponse.StatusCode == HttpStatusCode.OK;

         
            }
            catch (Exception Ex) {
                    
                System.Diagnostics.Debug.WriteLine("Error: " + Ex.Message);
                return Result;
            }

            
            
            return Result;

        }

        public bool Update(String status)
        {
            
            
                      
            try
            {
                // URL-encode the tweet...
                string tweet = HttpUtility.UrlEncode(status);

                // And send it off...
                string xml = oAuth.oAuthWebRequest(
                    oAuthTwitter.Method.POST,
                    "http://twitter.com/statuses/update.xml",
                    "status="+tweet);
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return false;
            }
            
            
            
            
            
            /*
            try
            {
                
                WebClient client = new WebClient { Credentials = new NetworkCredential { UserName = username, Password = password } };
                ServicePointManager.Expect100Continue = false;
                byte[] messageBody = Encoding.ASCII.GetBytes("status=" + status);
                client.UploadData("http://twitter.com/statuses/update.xml", messageBody);
                
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Error: " + ex.Message);
                return Resut;
            }
            
            Resut = true;
            return Resut;*/
        }
    }
}
